package cn.fenqing.demo;

import cn.fenqing.utils.ObjectUtils;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author fenqing
 */
public class ObjectUtilsDemo {

    public static void main(String[] args) {
        test1();
    }

    public static void test1(){
        //假数据
        ClassA classA = new ClassA();
        classA.setId(1);

        ClassB classB = new ClassB();
        ClassC classC = new ClassC();
        classC.setCUsername("我是cb里的cc");
        classB.setCc(classC);
        classB.setBUsername("我是a的cb");
        classA.setCb(classB);

        List<ClassB> bsList = Stream.iterate(0, i -> i + 1).limit(2).map(i -> {
            ClassB b = new ClassB();
            ClassC c = new ClassC();
            c.setCUsername("我是bsList[" + i + "]里的cc");
            b.setCc(classC);
            b.setBUsername("我是a的bsList[" + i + "]");
            return b;
        }).collect(Collectors.toList());
        classA.setBsList(bsList);

        ClassB[] bsArray = Stream.iterate(0, i -> i + 1).limit(2).map(i -> {
            ClassB b = new ClassB();
            ClassC c = new ClassC();
            c.setCUsername("bsArray[" + i + "]里的cc");
            b.setCc(classC);
            b.setBUsername("我是a的bsArray[" + i + "]");
            return b;
        }).toArray(i -> new ClassB[i]);
        classA.setBsArray(bsArray);

        Map<String, ClassB> bMap = Stream.iterate(0, i -> i + 1).limit(2).collect(Collectors.toMap(Object::toString, i -> {
            ClassB b = new ClassB();
            ClassC c = new ClassC();
            c.setCUsername("bMap[" + i + "]里的cc");
            b.setCc(classC);
            b.setBUsername("我是a的bMap[" + i + "]");
            return b;
        }));
        classA.setBMap(bMap);

        System.out.println(classA);
        classA.setCb(null);
        System.out.println(ObjectUtils.getValueDeep(classA, "id"));
        System.out.println(ObjectUtils.getValueDeep(classA, "cb"));
        System.out.println(ObjectUtils.getValueDeep(classA, "cb.bUsername"));
        System.out.println(ObjectUtils.getValueDeep(classA, "bsList[0].cc"));

    }

}

@Data
class ClassA {
    private Integer id;
    private ClassB cb;
    private List<ClassB> bsList;
    private ClassB[] bsArray;
    private Map<String, ClassB> bMap;
}

@Data
class ClassB {
    private String bUsername;
    private ClassC cc;
}

@Data
class ClassC {
    private String cUsername;
}